package com.slipper.bpmn.parser.support;

import com.slipper.bpmn.model.Bpmn;
import com.slipper.bpmn.model.Gateway;
import com.slipper.bpmn.parser.IParser;
import org.dom4j.Element;

public class GatewayParser implements IParser<Gateway> {
    @Override
    public Gateway parser(Element element) {
        Gateway gateway = parser(new Gateway(), element);
        if (Bpmn.BRANCH_NODE.equals(gateway.getType())) {
            if (gateway.getInLine().size() != 1) {
                throw new RuntimeException("分支节点[" + gateway.getName() + "]必须有且仅有一条流入线！");
            }
            if (gateway.getOutLine().size() < 2) {
                throw new RuntimeException("分支节点[" + gateway.getName() + "]必须至少有两条流出线！");
            }
        } else if (Bpmn.PARALLEL_NODE.equals(gateway.getType())) {
            if (gateway.getInLine().size() != 1) {
                throw new RuntimeException("并发开始节点[" + gateway.getName() + "]必须有且仅有一条流入线！");
            }
            if (gateway.getOutLine().isEmpty()) {
                throw new RuntimeException("并发开始节点[" + gateway.getName() + "]必须至少有一条流出线！");
            }
        } else if (Bpmn.MERGE_NODE.equals(gateway.getType())) {
            if (gateway.getInLine().isEmpty()) {
                throw new RuntimeException("并发结束节点[" + gateway.getName() + "]必须至少有一条流入线！");
            }
            if (gateway.getOutLine().size() != 1) {
                throw new RuntimeException("并发结束节点[" + gateway.getName() + "]必须有且仅有一条流出线！");
            }
        }
        return gateway;
    }
}
